package waggle.client.maps;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.maps.client.event.MarkerInfoWindowOpenHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.geom.Size;
import com.google.gwt.maps.client.overlay.Icon;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;

import waggle.client.bean.Occurrence;
import waggle.client.bean.OccurrenceSummary;
import waggle.client.bean.OccurrenceSummary.OccurrenceFieldItem;

public class OccurrenceMarker extends Marker {

	private final Occurrence occurrence;

	public OccurrenceMarker(Occurrence occurrence) {
		super(getPoint(occurrence), optionsManager.getOptions(occurrence));
		this.occurrence = occurrence;
		super.addMarkerInfoWindowOpenHandler(new MarkerInfoWindowOpenHandler() {
			public void onInfoWindowOpen(MarkerInfoWindowOpenEvent event) {
				// TODO Auto-generated method stub

			}

		});
	}

	private static LatLng getPoint(Occurrence occurrence) {
		double latitude = Double.parseDouble(occurrence.getDecimalLatitude());
		double longitude = Double.parseDouble(occurrence.getDecimalLongitude());
		return LatLng.newInstance(latitude, longitude);
	}

	private static OptionsManager optionsManager = new OptionsManager();

	private static class OptionsManager {
		private static Map<String, String> speciesMarkerUrls = new HashMap<String, String>();
		private static int index = 0;
		private static Icon baseIcon;

		OptionsManager() {
			baseIcon = Icon.newInstance();
			baseIcon
					.setShadowURL("http://www.google.com/mapfiles/shadow50.png");
			baseIcon.setIconSize(Size.newInstance(20, 34));
			baseIcon.setShadowSize(Size.newInstance(37, 34));
			baseIcon.setIconAnchor(Point.newInstance(9, 34));
			baseIcon.setInfoWindowAnchor(Point.newInstance(9, 2));
		}

		public String getIconUrl(Occurrence occurrence) {
			return speciesMarkerUrls.get(occurrence.getAcceptedSpecies());
		}

		public void resetIcons() {
			speciesMarkerUrls.clear();
			index = 0;
		}

		MarkerOptions getOptions(Occurrence occurrence) {
			MarkerOptions options = MarkerOptions
					.newInstance(getIcon(occurrence));
			options.setTitle("ReBioMa ID: " + occurrence.getId());
			return options;
		}

		private Icon getIcon(Occurrence occurrence) {
			OccurrenceFieldItem fieldItem = OccurrenceSummary
					.getDisplayField(occurrence);
			String species = fieldItem == null ? "----" : fieldItem.toString();
			String url = speciesMarkerUrls.get(species);
			if (url == null) {
				//因為這樣,可以讓每次都是從A開始,而且會持續下去
				final char letter = (char) ('A' + index++);
				url = "http://www.google.com/mapfiles/marker" + letter + ".png";
				speciesMarkerUrls.put(species, url);
			}
			Icon icon = Icon.newInstance(baseIcon);
			icon.setImageURL(url);
			return icon;
		}
	}
	
	  public Occurrence getOccurrence() {
		    return occurrence;
		  }

	  public static Map<String, String> getSpeciesMarkerUrlsMap() {
		    return OptionsManager.speciesMarkerUrls;
		  }
	  public static void resetIcons() {
		    optionsManager.resetIcons();
		  }
	  
	  public static boolean isMappable(Occurrence occurrence) {
		    try {
		      Double.parseDouble(occurrence.getDecimalLatitude());
		      Double.parseDouble(occurrence.getDecimalLongitude());
		      return true;
		    } catch (Exception e) {
		      return false;
		    }
		  }

}
